package aip.core.dao.information;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;

import aip.core.model.information.Catalog;

import com.googlecode.horncomb.bean.order.DefaultOrder;
import com.googlecode.horncomb.bean.order.DefaultOrderToken;
import com.googlecode.horncomb.bean.order.Order;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

/**
 * Catalog data access object
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class CatalogDao extends SqlMapEntityDaoImpl<Catalog>
{
    private static final long serialVersionUID = 1258900256122417566L;

    public static LinkedHashMap<Integer, Catalog> catalogMap = new LinkedHashMap<Integer, Catalog>();
    public static Map<Integer, Catalog> allCatalog = new HashMap<Integer, Catalog>();
    public static Map<Long, Catalog> longCatalog = new HashMap<Long, Catalog>();
    public static List<Catalog> menuList = new ArrayList<Catalog>();

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initDao() throws Exception
    {
        super.initDao();

        Order order = new DefaultOrder(new DefaultOrderToken("LEVEL_"),
                new DefaultOrderToken("ID_"));
        List<? extends Catalog> catalogs = super.queryAll(order);
        for (Catalog catalog : catalogs)
        {
            allCatalog.put(catalog.getId(), catalog);
            longCatalog.put(Long.valueOf(catalog.getId()), catalog);
            if (catalog.getLevel() == 1)
            {
                catalogMap.put(catalog.getId(), catalog);
                if (catalog.isShow())
                {
                    menuList.add(catalog);
                }
            }
            else
            {
                catalogMap.get(catalog.getSuperior()).getChildren()
                        .put(catalog.getId(), catalog);
            }
        }
    }

    /**
     * 根据上级栏目ID取栏目信息包含上级栏目
     */
    public List<Catalog> queryBySupId(Integer superiorId)
            throws DataAccessException
    {
        return (List<Catalog>) catalogMap.get(superiorId).getChildren()
                .values();
    }

    /**
     * 取有效的栏目，如传进为二级栏目，则直接返回二级栏目<br/>
     * 若传进为一级栏目，其存在二级栏目，则返回第一个二级栏目<br/>
     * 若传进为一级栏目，其不存在二级栏目，则返回该一级栏目
     * @param id
     * @return
     * @throws DataAccessException
     */
    public Catalog getRealCatalog(Integer id) throws DataAccessException
    {
        if (catalogMap.get(id) == null)
        {
            return allCatalog.get(id);
        }
        else
        {
            return this.getCatalogMap().get(id).getChildren().values()
                    .iterator().next();
        }
    }

    /**
     * @return the catalogMap
     */
    public LinkedHashMap<Integer, Catalog> getCatalogMap()
    {
        return catalogMap;
    }

    /**
     * @return the allCatalog
     */
    public Map<Integer, Catalog> getAllCatalog()
    {
        return allCatalog;
    }

    /**
     * @return the longCatalog
     */
    public static Map<Long, Catalog> getLongCatalog()
    {
        return longCatalog;
    }
}
